<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="检索信息" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="支付方式" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请选择">
          <el-option label="微信" value="1"></el-option>
          <el-option label="支付宝" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择">
          <el-option label="未支付" value="0"></el-option>
          <el-option label="已支付" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品信息" prop="goods">

        <el-select v-model="queryParams.goodsId" placeholder="请选择">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker clearable v-model="queryParams.orderTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['store:order:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['store:order:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['store:order:remove']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" width="50" label="编号"/>
      <el-table-column label="订单号" align="center" prop="orderId"/>
      <el-table-column label="收货人" align="center" prop="consignee"/>
      <el-table-column label="下单用户" align="center" prop="memberName"/>
      <el-table-column label="商品" align="center" prop="goodsName">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.goodsName != null">
            {{ scope.row.goodsName }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无商品</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="goodsImage">
        <template slot-scope="scope">
          <el-image :src="scope.row.goodsImage" fit="cover" style=" width:50; height:50;">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="支付方式" align="center" prop="payMethod">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.payMethod != null">
            {{ scope.row.payMethod == '2' ? '支付宝' : '微信' }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <el-tag effect="dark" :type="scope.row.payStatus == '0' ? 'info' : 'danger'"
                  v-if="scope.row.payStatus != null">
            {{ scope.row.payStatus == '0' ? '未支付' : '已支付' }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.amount != null">
            {{ scope.row.amount }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" width="110">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.phone != null">
            {{ scope.row.phone }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" width="200">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.address != null">
            {{ scope.row.address }}
          </el-tag>
          <el-tag effect="dark" type="info" v-else>暂无地址</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['store:order:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['store:order:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderId" v-if="this.form.orderId != null">
          <el-input v-model="form.orderId" :disabled="true"/>
        </el-form-item>
        <el-form-item label="下单用户" prop="memberId">
          <el-select v-model="form.memberId" placeholder="请选择下单用户" @visible-change="handChangeMenber">
            <el-option v-for="item in memberList" :key="item.id" :label="item.nickName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" prop="goodsId">
          <el-select v-model="form.goodsId" placeholder="请选择商品">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="addressId">
          <el-select v-model="form.addressId" placeholder="请选择地址">
            <el-option v-for="item in AddressList" :key="item.id" :label="item.addressName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="支付方式" prop="payMethod">
          <el-radio-group v-model="form.payMethod">
            <el-radio :label="'1'">微信支付</el-radio>
            <el-radio :label="'2'" checked>支付宝支付</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <el-radio-group v-model="form.payStatus">
            <el-radio :label="'0'">未支付</el-radio>
            <el-radio :label="'1'" checked>已支付</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品数量" prop="count">
          <el-input-number v-model="form.count" @change="handleChangeCount" :min="1" :max="99999"
                           label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="实收金额" prop="amount">
          <el-input-number v-model="form.amount" @change="handleChangeAmount" :min="0" :max="99999"
                           label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="手机号" prop="phone" v-if="form.phone">

          <el-input v-model="form.phone" placeholder="请输入手机号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address" v-if="form.address">
          <el-input v-model="form.address" placeholder="请输入地址" :disabled="true"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/store/order";
import {GoodsList} from "@/api/store/goods"
import {getMemberList} from "@/api/store/member"
import {getAddressListByMemberId} from "@/api/store/address"

export default {
  name: "Order",
  data() {
    return {
      // 用户信息
      memberList: [],
      // 地址信息
      AddressList: [],
      // 商品信息
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        payMethod: '2',
        count: '1',
        amount: '0',
        payStatus: '0'

      },
      // 表单校验
      rules: {
        status: [
          {
            required: true,
            message: "订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消 7退款不能为空",
            trigger: "change"
          }
        ],
        memberId: [
          {required: true, message: "下单用户不能为空", trigger: "blur"}
        ],
        orderTime: [
          {required: true, message: "下单时间不能为空", trigger: "blur"}
        ],
        payMethod: [
          {required: true, message: "支付方式 1微信,2支付宝不能为空", trigger: "blur"}
        ],
        payStatus: [
          {required: true, message: "支付状态 0未支付 1已支付 2退款不能为空", trigger: "change"}
        ],
        count: [
          {required: true, message: "商品数量不能为空", trigger: "blur"}
        ],

      }
    };
  },
  created() {
    this.getList();
    this.getGoodsList();
    this.MemberList();
  },
  methods: {
    // 获取地址信息
    getAddressList(memberId) {
      if (memberId) {
        getAddressListByMemberId(memberId).then(res => {
          this.AddressList = res.data
        })
      }
    },
    // 获取用户列表
    MemberList() {
      getMemberList().then(res => {
        this.memberList = res.data
      })
    },
    handChangeMenber(val) {
      if (!val) {
        this.getAddressList(this.form.memberId)
      }
    },
    handleChangeCount(value) {
      this.form.count = value;
    },
    handleChangeAmount(value) {
      this.form.amount = value;
    },
    // 获取商品列表
    getGoodsList() {
      GoodsList().then(res => {
        this.options = res.data
      })
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payMethod: '2',
        count: '1',
        amount: '0',
        payStatus: '0'
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.getAddressList(this.form.memberId)
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addOrder(this.form).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
<style lang="scss" scoped>
.el-image {
  border-radius: 5px;
  background-color: #ebeef5;
  box-shadow: 0 0 5px 1px #ccc;

  ::v-deep .el-image__inner {
    transition: all 0.3s;
    cursor: pointer;

    &:hover {
      transform: scale(1.2);
    }
  }

  ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    color: #909399;
    font-size: 30px;
  }
}
</style>
